<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

if (!function_exists('get_array_column')) {

    /**
     * 获取2维数组指定一列的值
     *
     * @access public
     * @param $array_object 二维数组
     * @param $column_name  列名或列编号
     * @return array
     */
    function get_array_column($array_object = array(), $column_name = '') {

        $result_array = array();
        if (count($array_object) == 0 || strlen($column_name) <= 0) {
            return $result_array;
        }
        foreach ($array_object as $row) {
            if (is_array($row)) {
                $result_array[] = $row[$column_name];
            }
        }
        return $result_array;
    }

}

/** 将结果集中的指定一列作为数组的key返回
 * 
 */
if (!function_exists('get_array_given_key')) {

    function get_array_given_key($array_object = array(), $column_name = '') {

        $result_array = array();
        if (count($array_object) == 0 || strlen($column_name) <= 0) {
            return $result_array;
        }
        foreach ($array_object as $row) {
            $result_array[$row[$column_name]] = $row;
        }
        return $result_array;
    }

}
/**
 * 删除数组中指定的key为指定值
 */
if (!function_exists('get_array_unset')) {

    function get_array_unset($array_object = array(), $column_name, $column_value = array()) {

        $result_array = array();
        if (count($array_object) == 0 || strlen($column_name) <= 0) {
            return $result_array;
        }
        $i = 0;
        foreach ($array_object as $k => $row) {
            if (in_array($row[$column_name], $column_value)) {
                //unset($array_object[$k]);
            } else {
                $result_array[$i++] = $row;
            }
        }
        return $result_array;
    }

}

if (!function_exists('change_to_idtext_arr')) {

    /**
     * 将一数组转化为id, text 格式的二维数组
     */
    function change_to_idtext_arr($array_object = array(), $key_flag = FALSE) {
        $result_array = array();
        $i = 0;
        if ($key_flag == TRUE) {
            foreach ($array_object as $key => $val) {
                $result_array[$i] = array('id' => "$key", 'text' => $val);
                $i++;
            }
        } else {
            foreach ($array_object as $val) {
                $result_array[$i] = array('id' => "$val", 'text' => $val);
                $i++;
            }
        }

        return $result_array;
    }

}

if (!function_exists('rows_sort')) {

    /**
     * 将二维数组按照指定字段排序
     * @param type $records 二维数组
     * @param type $field 排序字段
     * @param type $cmp_asint 是否按照数字比较
     * @param type $reverse 是否逆序
     * @return type
     */
    function rows_sort($records, $field, $cmp_asint = FALSE, $reverse = FALSE) {
        $hash = array();

        foreach ($records as $record) {
            $hash[$record[$field]] = $record;
        }
        if ($cmp_asint)
            $sort_flag = SORT_NUMERIC;
        else
            $sort_flag = SORT_REGULAR;
        if ($reverse)
            krsort($hash, $sort_flag);
        else
            ksort($hash, $sort_flag);

        $records = array();

        foreach ($hash as $record) {
            $records[] = $record;
        }
        
        return $records;
    }

}